<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class media_env_highway_manage extends CI_Controller {


	public function __construct() {
		session_start();
		parent::__construct();
		$this->load->helper("common");
	}

	public function  index(){
		$this->env_highway();
	}
	public function env_highway(){


		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();

		$this->load->library("common/right_manage");
		$data['query_highway_env']	=false;
		if ($this->right_manage->check_right("query_highway_env")) {
			$data['query_highway_env']	=true;
		}
		$data['delete_highway_env']	=false;
		if ($this->right_manage->check_right("delete_highway_env")) {
			$data['delete_highway_env']=true;
		}
		$data['add_highway_env']	=false;
		if ($this->right_manage->check_right("add_highway_env")) {
			$data['add_highway_env']	=true;
		}
		$data['update_highway_env']	=false;
		if ($this->right_manage->check_right("update_highway_env")) {
			$data['update_highway_env']	=true;
		}
		$search['province'] 	= $this->input->get("province");
		$search['city']			= $this->input->get("city");
		$search['region'] 		= $this->input->get("region");
		$search['keyword'] 		= $this->input->get("keyword");
		
		
		$this->load->library("env/env_highway");
		$data['env_highway_list'] 	=$this->env_highway->query_highway_list($_SESSION['user_id'],$search);
		$data['num']				=$this->env_highway->query_highway_num($_SESSION['user_id'],$search);
		
		
		
		$this->load->library("sys/region");
		$data['province_list']	= $this->region->query_user_top_regions($_SESSION['user_id']);
		$data['search']=$search;
		$search_url =format_search_url($search);
		$this->load->library("tools/pagination");
		$data['page_link']=$this->pagination->get_pagination(base_url()."index.php/media_env_highway_manage/env_highway?".$search_url,$data['num']);
		

		$this->load->view("media/env_highway/env_highway_main",$data);
	}
	public function add_env_highway(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("add_highway_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$this->load->library("sys/region");
		$data['top_regions']	= $this->region->query_user_top_regions($_SESSION['user_id']);

		$this->load->view("media/env_highway/add_env_highway",$data);
	}
	public function save_env_highway(){

		$this->load->library("common/login_manage");
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("add_highway_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			echo json_encode($data);die();
		}
		$highway['highway_name'] =trim($this->input->post("highway_name"));
		$highway['province'] =trim($this->input->post("province"));
		$highway['highway_num'] =trim($this->input->post("highway_num"));
		$highway['highway_length'] =trim($this->input->post("highway_length"));
		$highway['along_area'] =trim($this->input->post("along_area"));
		$highway['create_by'] =$_SESSION['user_name'];
		$highway['create_time'] =date("Y-m-d H:i:s");
		$this->load->library("env/env_highway");
		$result =$this->env_highway->save_env_highway($highway);
		$this->load->helper("common");
		echo JSON($result);
	}

	public function query_env_urban_circle(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("query_highway_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$brand_id =$this->input->get("brand_id");
		$this->load->library("sys/brand");
		$data['brand_info']=$this->brand->query_brand_info($brand_id);

		$this->load->view("media/env_highway/query_env_urban_circle",$data);
	}


	public function edit_env_highway(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("update_highway_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$highway_id =$this->input->get("highway_id");
		$this->load->library("env/env_highway");
		$data['env_highway_info']=$this->env_highway->query_env_highway($highway_id);


		$this->load->library("sys/region");
		$data['top_regions']	= $this->region->query_user_top_regions($_SESSION['user_id']);
		$this->load->view("media/env_highway/edit_env_highway",$data);
	}

	public function update_env_highway(){

		$this->load->library("common/login_manage");
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}


		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("update_highway_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			echo json_encode($data);die();
		}
		$highway_id =$this->input->post('highway_id');

		$highway['highway_name'] =trim($this->input->post("highway_name"));
		$highway['province'] =trim($this->input->post("province"));
		$highway['highway_num'] =trim($this->input->post("highway_num"));
		$highway['highway_length'] =trim($this->input->post("highway_length"));
		$highway['along_area'] =trim($this->input->post("along_area"));


		$highway['modify_by'] =$_SESSION['user_name'];
		$highway['modify_time'] =date("Y-m-d H:i:s");

		$this->load->library("env/env_highway");
		$result =$this->env_highway->update_env_highway($highway,$highway_id);
		$this->load->helper("common");
		echo JSON($result);
	}

	public function delete_env_highway() {
		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();


		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("delete_highway_env",$_SESSION['user_id'])) {

			$this->load->library("common/user_menu");
			$data['user_menu']=$this->user_menu->get_user_menu();
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}
		$this->load->library("env/env_highway");
		$highway_id =$this->input->get('highway_id');
		$this->env_highway->delete_env_highway($highway_id);
		echo "<script>history.go(-1)</script>";
	}


	public function highway_section(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();


		$this->load->library("common/right_manage");
		$data['query_highway_env']	=false;
		if ($this->right_manage->check_right("query_highway_env")) {
			$data['query_highway_env']	=true;
		}
		$data['delete_highway_env']	=false;
		if ($this->right_manage->check_right("delete_highway_env")) {
			$data['delete_highway_env']=true;
		}
		$data['add_highway_env']	=false;
		if ($this->right_manage->check_right("add_highway_env")) {
			$data['add_highway_env']	=true;
		}
		$data['update_highway_env']	=false;
		if ($this->right_manage->check_right("update_highway_env")) {
			$data['update_highway_env']	=true;
		}


		$highway_id =$this->input->get("highway_id");

		$this->load->library("env/highway_section");
		$data['highway_section_list'] 	=$this->highway_section->query_section_list($highway_id);
		$data['num']					=$this->highway_section->query_section_num($highway_id);

		$this->load->library("tools/pagination");
		$data['page_link']=$this->pagination->get_pagination(base_url()."index.php/media_env_highway_manage/highway_section?highway_id=".$highway_id,$data['num']);

		$this->load->library("env/env_highway");
		$data['env_highway_info']=$this->env_highway->query_env_highway($highway_id);

		$this->load->view("media/env_highway/highway_section",$data);
	}


	public function add_highway_section(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();


		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("add_highway_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$highway_id =$this->input->get("highway_id");
		$this->load->library("env/env_highway");
		$data['env_highway_info']=$this->env_highway->query_env_highway($highway_id);

		$this->load->view("media/env_highway/add_highway_section",$data);
	}

	public function save_highway_section(){
		$this->load->library("common/login_manage");
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("delete_highway_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			echo json_encode($data);die();
		}
		$section['highway_id'] =trim($this->input->post("highway_id"));
		$section['section_name'] =trim($this->input->post("section_name"));
		$section['level'] =trim($this->input->post("level"));
		$section['section_length'] =$this->input->post("section_length");
		$section['traffic'] =trim($this->input->post("traffic"));
		$section['price_range'] =trim($this->input->post("price_range"));

		$section['create_by'] =$_SESSION['user_name'];
		$section['create_time'] =date("Y-m-d H:i:s");
		$this->load->library("env/highway_section");
		$result =$this->highway_section->save_highway_section($section);
		$this->load->helper("common");
		echo JSON($result);
	}

	public function edit_highway_section() {

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("update_highway_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}


		$section_id =$this->input->get("section_id");
		$this->load->library("env/highway_section");
		$data['highway_section_info']=$this->highway_section->query_highway_section_info($section_id);

		$this->load->view("media/env_highway/edit_highway_section",$data);
	}

	public function update_highway_section(){

		$this->load->library("common/login_manage");
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("update_highway_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			echo json_encode($data);die();
		}


		$section_id =$this->input->post("section_id");
		$section['highway_id'] =trim($this->input->post("highway_id"));
		$section['section_name'] =trim($this->input->post("section_name"));
		$section['level'] =trim($this->input->post("level"));
		$section['section_length'] =$this->input->post("section_length");
		$section['traffic'] =trim($this->input->post("traffic"));
		$section['price_range'] =trim($this->input->post("price_range"));

		$section['modify_by'] =$_SESSION['user_name'];
		$section['modify_time'] =date("Y-m-d H:i:s");
		$this->load->library("env/highway_section");
		$result =$this->highway_section->update_highway_section($section,$section_id);
		$this->load->helper("common");
		echo JSON($result);

	}

	public function delete_highway_section(){
		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("delete_highway_env",$_SESSION['user_id'])) {
			$this->load->library("common/user_menu");
			$data['user_menu']=$this->user_menu->get_user_menu();
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}


		$this->load->library("env/highway_section");
		$section_id =$this->input->get('section_id');
		$this->highway_section->delete_highway_section($section_id);
		echo "<script>history.go(-1)</script>";

	}

	public function highway_point(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();


		$this->load->library("common/right_manage");
		$data['query_highway_env']	=false;
		if ($this->right_manage->check_right("query_highway_env")) {
			$data['query_highway_env']	=true;
		}
		$data['delete_highway_env']	=false;
		if ($this->right_manage->check_right("delete_highway_env")) {
			$data['delete_highway_env']=true;
		}
		$data['add_highway_env']	=false;
		if ($this->right_manage->check_right("add_highway_env")) {
			$data['add_highway_env']	=true;
		}
		$data['update_highway_env']	=false;
		if ($this->right_manage->check_right("update_highway_env")) {
			$data['update_highway_env']	=true;
		}

		$section_id =$this->input->get("section_id");

		$this->load->library("env/highway_point");
		$data['highway_point_list'] 	=$this->highway_point->query_point_list($section_id);
		$data['num']					=$this->highway_point->query_point_num($section_id);

		$this->load->library("tools/pagination");
		$data['page_link']=$this->pagination->get_pagination(base_url()."index.php/media_env_highway_manage/highway_point?section_id=".$section_id,$data['num']);

		$this->load->library("env/highway_section");
		$data['highway_section_info']=$this->highway_section->query_highway_section_info($section_id);

		$this->load->view("media/env_highway/highway_point",$data);
	}

	public function add_highway_point(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();



		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("add_highway_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$section_id =$this->input->get("section_id");
		$this->load->library("env/highway_section");
		$data['highway_section_info']=$this->highway_section->query_highway_section_info($section_id);

		$this->load->library("sys/region");
		$data['top_regions']	= $this->region->query_user_top_regions($_SESSION['user_id']);

		$this->load->view("media/env_highway/add_highway_point",$data);
	}
	public function save_highway_point(){

		$this->load->library("common/login_manage");
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}


		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("add_highway_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			echo json_encode($data);die();
		}
		$point['highway_id'] =trim($this->input->post("highway_id"));
		$point['section_id'] =trim($this->input->post("section_id"));
		$point['point_name'] =trim($this->input->post("point_name"));
		$point['point_attr'] =trim($this->input->post("point_attr"));
		$point['location'] =$this->input->post("location");
		$point['province'] =trim($this->input->post("province"));
		$point['city'] =trim($this->input->post("city"));
		$point['region'] =trim($this->input->post("region"));
		$point['distribution_map'] =trim($this->input->post("distribution_map"));

		$point['create_by'] =$_SESSION['user_name'];
		$point['create_time'] =date("Y-m-d H:i:s");
		$this->load->library("env/highway_point");
		$result =$this->highway_point->save_highway_point($point);
		$this->load->helper("common");
		echo JSON($result);
	}

	public function edit_highway_point(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("update_highway_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$point_id =$this->input->get("point_id");
		$this->load->library("env/highway_point");
		$data['env_highway_point_info']=$this->highway_point->query_highway_point_info($point_id);


		$this->load->library("sys/region");
		$data['top_regions']	= $this->region->query_user_top_regions($_SESSION['user_id']);
		$data['second_regions'] =null;
		$data['last_regions']	=null;
		if($data['env_highway_point_info']['city']){
			$data['second_regions'] =$this->region->get_regions($data['env_highway_point_info']['province']);
		}
		if($data['env_highway_point_info']['region']){
			$data['last_regions'] =$this->region->get_regions($data['env_highway_point_info']['city']);
		}
		$this->load->view("media/env_highway/edit_highway_point",$data);
	}

	public function update_highway_point(){

		$this->load->library("common/login_manage");
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("update_highway_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			echo json_encode($data);die();
		}
		$point_id =$this->input->post("point_id");
		$point['highway_id'] =trim($this->input->post("highway_id"));
		$point['section_id'] =trim($this->input->post("section_id"));
		$point['point_name'] =trim($this->input->post("point_name"));
		$point['point_attr'] =trim($this->input->post("point_attr"));
		$point['location'] =$this->input->post("location");
		$point['province'] =trim($this->input->post("province"));
		$point['city'] =trim($this->input->post("city"));
		$point['region'] =trim($this->input->post("region"));
		$point['distribution_map'] =trim($this->input->post("distribution_map"));

		$point['modify_by'] =$_SESSION['user_name'];
		$point['modify_time'] =date("Y-m-d H:i:s");
		$this->load->library("env/highway_point");
		$result =$this->highway_point->update_highway_point($point,$point_id);
		$this->load->helper("common");
		echo JSON($result);
	}
	public function delete_highway_point(){
		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("delete_highway_env",$_SESSION['user_id'])) {
			$this->load->library("common/user_menu");
			$data['user_menu']=$this->user_menu->get_user_menu();
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$this->load->library("env/highway_point");
		$point_id =$this->input->get('point_id');
		$this->highway_point->delete_highway_point($point_id);
		echo "<script>history.go(-1)</script>";

	}

	public function get_highway_list(){
		$province =$this->input->get("province");
		$this->load->library("env/env_highway");
		$result =$this->env_highway->get_all_highway();
		echo json_encode($result);
	}
	public function get_section_list(){
		$highway_id =$this->input->get("highway_id");
		$this->load->library("env/highway_section");
		$result =$this->highway_section->get_all_section($highway_id);
		echo json_encode($result);
	}
	public function get_point_list(){
		$section_id =$this->input->get("section_id");
		$this->load->library("env/highway_point");
		$result =$this->highway_point->get_all_point($section_id);
		echo json_encode($result);
	}

	public function query_env_highway() {

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();

		$highway_id =$this->input->get("highway_id");
		$this->load->library("env/env_highway");
		$data['env_highway_info']=$this->env_highway->query_env_highway($highway_id);

		$this->load->library("sys/region");
		$data['top_regions']	= $this->region->query_user_top_regions($_SESSION['user_id']);
		$this->load->view("media/env_highway/query_env_highway",$data);
	}

	public function query_highway_section(){


		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();

		$section_id =$this->input->get("section_id");
		$this->load->library("env/highway_section");
		$data['highway_section_info']=$this->highway_section->query_highway_section_info($section_id);

		$this->load->view("media/env_highway/query_highway_section",$data);

	}
	public function query_highway_point(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();

		$point_id =$this->input->get("point_id");
		$this->load->library("env/highway_point");
		$data['env_highway_point_info']=$this->highway_point->query_highway_point_info($point_id);


		$this->load->library("sys/region");
		$data['top_regions']	= $this->region->query_user_top_regions($_SESSION['user_id']);
		$data['second_regions'] =null;
		$data['last_regions']	=null;
		if($data['env_highway_point_info']['city']){
			$data['second_regions'] =$this->region->get_regions($data['env_highway_point_info']['province']);
		}
		if($data['env_highway_point_info']['region']){
			$data['last_regions'] =$this->region->get_regions($data['env_highway_point_info']['city']);
		}
		$this->load->view("media/env_highway/query_highway_point",$data);
	}
}